package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.TravelDetailsMapper;
import cn.com.thinvent.dispatch.dao.TravelManagementMapper;
import cn.com.thinvent.dispatch.entity.TravelDetails;
import cn.com.thinvent.dispatch.entity.TravelManagement;
import cn.com.thinvent.dispatch.entity.request.TravelDetailsRequest;
import cn.com.thinvent.dispatch.entity.request.TravelManagementRequest;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.TravelManagementService;
import cn.com.thinvent.dispatch.service.biz.util.DataConvertUtils;
import cn.com.thinvent.dispatch.service.biz.util.KeyGenerator;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class TravelManagementServiceImpl implements TravelManagementService {

    @Resource
    private TravelManagementMapper travelManagementMapper;

    @Resource
    private TravelDetailsMapper travelDetailsMapper;

    @Override
    public TravelManagement save(TravelManagementRequest travelManagementRequest) {
        List<TravelDetailsRequest> travelDetails = travelManagementRequest.getTravelDetailsRequests();
        TravelManagement travelManagement = new TravelManagement();
        List<TravelDetails> travelDetailsList = new ArrayList<>();
        DataConvertUtils.convertDTO2DO(travelManagementRequest, travelManagement);
        DataConvertUtils.convertDOList2DTOList(travelDetails, travelDetailsList, TravelDetails.class);

        String travelManagementId = KeyGenerator.generateKey();
        Date date = new Date();
        travelManagement.setId(travelManagementId);
        travelManagement.setCreatedAt(date);
        travelManagement.setUpdatedAt(date);
        travelManagementMapper.insertTravelManagement(travelManagement);

        travelDetailsList.stream().forEach(travelDetails1 -> {
            travelDetails1.setId(KeyGenerator.generateKey());
            travelDetails1.setTravelManagementId(travelManagementId);
            travelDetails1.setCreatedAt(date);
            travelDetails1.setUpdatedAt(date);
            travelDetailsMapper.insertTravelDetails(travelDetails1);
        });
        return travelManagement;
    }

    @Override
    public void deleteById(String id) {
        travelDetailsMapper.deleteByTravelManagementId(id);
        travelManagementMapper.deleteTravelManagementById(id);
    }

    @Override
    public TravelManagementRequest findById(String id) {
        TravelManagement travelManagement = travelManagementMapper.selectTravelManagementById(id);
        List<TravelDetails> travelDetails = travelDetailsMapper.selectByTravelManagementId(id);

        TravelManagementRequest travelManagementRequest = new TravelManagementRequest();
        List<TravelDetailsRequest> travelDetailsList =  new ArrayList<>();
        DataConvertUtils.convertDO2DTO(travelManagement, travelManagementRequest);
        DataConvertUtils.convertDOList2DTOList(travelDetails, travelDetailsList, TravelDetailsRequest.class);
        travelManagementRequest.setTravelDetailsRequests(travelDetailsList);
        return travelManagementRequest;
    }

    @Override
    public TravelManagement update(TravelManagementRequest travelManagementRequest) {
        TravelManagement travelManagement = new TravelManagement();
        DataConvertUtils.convertDTO2DO(travelManagementRequest, travelManagement);
        List<TravelDetails> details = new ArrayList<>();
        List<TravelDetailsRequest> travelDetails = travelManagementRequest.getTravelDetailsRequests();
        DataConvertUtils.convertDOList2DTOList(travelDetails, details, TravelDetails.class);

        details.stream().forEach(travelDetails1 -> {
            travelDetails1.setTravelManagementId(travelManagementRequest.getId());
        });

        // 删除老的详情
        travelDetailsMapper.deleteByTravelManagementId(travelManagementRequest.getId());
        // 新增数据
        travelManagementMapper.insertTravelManagement(travelManagement);
        // 新增详情数据
        details.stream().forEach(travelDetails1 -> {
            travelDetailsMapper.insertTravelDetails(travelDetails1);
        });
        return travelManagement;
    }

    @Override
    public PagedGridResult<TravelManagement> getTravelList(int page, int pageSize, String tripName, Date startDate, Date endDate) {
        // 计算 OFFSET
        int offset = (page - 1) * pageSize;

        // 查询当前页的数据
        List<TravelManagement> list = travelManagementMapper.selectByPage(offset, pageSize, tripName, startDate, endDate);

        // 查询总记录数
        long totalRecords = travelManagementMapper.countAll(tripName, startDate, endDate);

        // 计算总页数
        int totalPages = (int) Math.ceil((double) totalRecords / pageSize);

        // 封装分页结果
        PagedGridResult<TravelManagement> pagedResult = new PagedGridResult<>();
        pagedResult.setPage(page);
        pagedResult.setTotal(totalPages);
        pagedResult.setRecords(totalRecords);
        pagedResult.setRows(list);

        return pagedResult;
    }
}
